<template>
  <div class="container">
    <el-row>
      <el-col :span="12" :xs="0">左半边占位</el-col>
      <el-col :span="12" :xs="24">
        <el-form :model="form" :rules="rules" class="login-form">
          <h1>Hello</h1>
          <h2>欢迎登录</h2>

          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              type="password"
              placeholder="请输入密码"
              :prefix-icon="Lock"
            ></el-input>
          </el-form-item>
          <el-form-item prop="button">
            <el-button
              type="primary"
              size="default"
              class="login_btn"
              :loading="loading"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
  import Pagination from '@/components/Pagination/index.vue'
  import SvgIcon from '@/components/SvgIcon/index.vue'
  import { User, Lock } from '@element-plus/icons-vue'
  import type { FormInstance, FormRules } from 'element-plus'
  import { useRouter } from 'vue-router'
  let $router = useRouter()

  // Import statements
  import { ref, computed, onMounted, reactive } from 'vue'
  import useUserStore from '@/store/modules/user'

  let userStore = useUserStore()

  let loading = ref(false)

  interface RuleForm {
    username: string
    password: string
  }

  const form: string = reactive({
    username: '',
    password: '',
  })

  const rules = reactive<FormRules<RuleForm>>({
    username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  })

  const login = async () => {
    loading.value = true
    try {
      await userStore.userLogin(form)
      loading.value = false
      $router.push('/home')
    } catch (error) {
      console.log(error)
      loading.value = false
    } finally {
      loading.value = false
    }
  }
</script>

<style scoped lang="scss">
  .container {
    width: 100%;
    height: 100vh;
    background: url('@/assets/images/background.jpg') no-repeat;
    background-size: cover;
    position: relative;

    .login-form {
      position: relative;
      width: 80%;
      top: 30vh;
      background: url('@/assets/images/login_form.png') no-repeat;
      background-size: cover;
      padding: 40px;
      h1 {
        color: white;
        font-size: 40px;
      }
      h2 {
        color: white;
        font-size: 20px;
        margin: 20px 0;
      }
      .login_btn {
        width: 100%;
        color: white;
      }
      .el-form-item {
        margin-bottom: 25px; // Adjust as needed
      }
    }
  }
</style>
